<template>
  <div class="account-table-wrap">
    <el-table
      :data="tableData"
      style="width: 100%"
      :border="true"
      :fit="true"
      :stripe="true"
      size="mini"
      align="center">
      <el-table-column
        prop="id"
        label="id"
        min-width="60"
        align="center">
      </el-table-column>

      <el-table-column
        :show-overflow-tooltip="true"
        prop="number"
        label="商品编号"
        min-width="80"
        align="center">
      </el-table-column>

      <el-table-column
      :show-overflow-tooltip="true"
        prop="class_name"
        label="分类"
        min-width="80"
        align="center">
      </el-table-column>

      <el-table-column
      :show-overflow-tooltip="true"
        prop="goods_name"
        label="名称"
        min-width="90"
        align="center">
      </el-table-column>

      <el-table-column
      :show-overflow-tooltip="true"
        prop="model"
        label="型号"
        min-width="90"
        align="center">
      </el-table-column>

      <el-table-column
      :show-overflow-tooltip="true"
        prop="unit"
        label="单位"
        min-width="60"
        align="center">
      </el-table-column>

      <el-table-column
        label="单价"
        min-width="90"
        align="center">
        <template slot-scope="scope">
          <el-button
            plain
            size="mini"
            type="primary"
            @click="priceEdit(scope.row)">{{scope.row.price}}</el-button>
        </template>
      </el-table-column>

      <el-table-column
      :show-overflow-tooltip="true"
        prop="materials_count"
        label="配料项"
        min-width="60"
        align="center">
      </el-table-column>

      <el-table-column
        label="产品状态"
        min-width="90"
        align="center">
        <template slot-scope="scope">
          <el-button  plain size="mini" 
            @click="clickChange(scope.$index, scope.row)" 
            :type="scope.row.status===1?'success':'warning'" 
            v-text="scope.row.status===1?'上架':'下架'">
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作"
      min-width="340"
      align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            plain
            type="info"
            @click="peiliao(scope.row)">配料清单</el-button>
          <el-button
            size="mini"
            plain
            type="primary"
            @click="tableEdit(scope.row)">编辑</el-button>
          <el-button
            class="ex-btn"
            size="mini"
            plain
            type="warning"
            @click="priceLog(scope.row)">单价记录</el-button>
          <el-button
            size="mini"
            plain
            type="danger"
            @click="tableDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <deleteConfirm v-if="showMask" @closeDelete="closeDelete" @deleteConfirm="deleteConfirm"></deleteConfirm>
    <change v-if="showChange" :data="statusData" @closeChange="closeChange" @statusConfirm="statusConfirm"></change>
    <price v-if="showPrice" :data="priceData" @closeChange="closePrice" @priceConfirm="priceConfirm"></price>
    <priceChange :logData="priceLogData" v-if="showPriceLog" @close="priceLogClose"></priceChange>
  </div>
</template>
<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Prop, Emit } from 'vue-property-decorator';
import deleteConfirm from '@/components/deleteConfirm.vue';
import ajaxResquest from '@/api/apiRequest';
import change from '@/components/changeStatus.vue';
import price from '@/components/changePrice.vue';
import priceChange from '@/tables/goods/priceChangeTable.vue';

@Component({
  components: {
    deleteConfirm,
    change,
    price,
    priceChange
  }
})
export default class AccountListTable extends Vue {
  // tableData: Array<any> = [];
  @Prop({
    type: Array
  })
  tableData!: any[];
  showMask: boolean = false;
  deleteId!: number;
  deleteRes!: ajaxResquest;
  changeRes!: ajaxResquest;
  showChange: boolean = false;
  statusData: any = {};

  showPrice: boolean = false;
  priceData: any = {};
  showPriceLog: boolean = false;
  priceLogData: any = {};
  @Emit('tableEdit')
  tableEdit(row: any) {
    console.log(row);
    this.$to('produceListCreate', { edit: true, id: row.id });
  }
  mounted() {
    this.deleteRes = new ajaxResquest('/goods/delGoods');
    this.changeRes = new ajaxResquest('/goods/updateGoodsStatus');
  }
  tableDelete(row: any) {
    console.log(row);
    this.deleteId = row.id;
    this.showMask = true;
  }
  closeDelete(row: object) {
    this.showMask = false;
  }
  deleteConfirm() {
    console.log('done');
    this.deleteRes.getFunction({ goodsId: this.deleteId }).then((res:any) => {
      if (res.data.code === 1) {
        this.getData();
        this.showMask = false;
      }
    });
  }
  clickChange(index: number, row: any) {
    this.statusData = row;
    this.showChange = true;
  }
  priceEdit(row: any) {
    this.priceData = row;
    this.showPrice = true;
  }
  closeChange() {
    this.showChange = false;
  }
  peiliao(row: any) {
    this.$to('goodSupList', { id: row.id });
  }
  statusConfirm() {
    let data = { goodsId: this.statusData.id, status: this.statusData.status };
    this.changeRes.getFunction(data).then((res:any) => {
      if (res.data.code === 1) {
        this.showChange = false;
        this.getData();
      }
    });
  }
  closePrice() {
    this.showPrice = false;
  }
  priceConfirm() {
    this.getData();
    this.showPrice = false;
  }
  @Emit('getData')
  getData() {}
  priceLog(row: any) {
    // console.log(row);

    this.priceLogData = row;
    console.log(this.priceLogData);

    this.showPriceLog = true;
  }
  priceLogClose() {
    this.showPriceLog = false;
  }
}
</script>
 <style lang="less">
.account-table-wrap {
  .ex-btn {
    // margin: 10px 0 0 0;
  }
}
</style>